/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Cart;

import DAO.DAO;
import DTO.Category;
import DTO.Item;
import Exception.KwickKioskDBException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * this class implements  search service functionalities
 * defined in {@link ISearchManager} interface
 * @author tejinder
 * @see Cart
 */
public class SearchManager implements ISearchManager {

    private DAO dao;
    private static final Logger log =Logger.getLogger(SearchManager.class.getName());

    /**
     * constructor for this class, injecting data access objects
     * @param dao
     */
    public SearchManager(DAO dao) {
        this.dao = dao;
    }

    /**
     * this method process search request for all categories.
     * @return
     */
    public List<Category> getCategories() {

        List<Category> categories = null;
        try {
            categories = dao.getCategories();
        } catch (KwickKioskDBException ex) {
            System.out.println("NEED TO IMPLEMENT error handling here");
           log.log(Level.SEVERE, null, ex);
        }
        return categories;

    }

    /**
     *This method processes request for search for an item by a criteria
     * like name, make, model and description
     * @param keyword
     * @param criteria
     * @return
     */
    public List<Item> searchBykeyWord(String keyword, String criteria) {

           List<Item> newItemList = null;
        try {
            if("model".equalsIgnoreCase(criteria)){
                newItemList = dao.searchByModel(keyword, "WebStore");
            }else if("make".equalsIgnoreCase(criteria)){
                newItemList = dao.searchByMake(keyword, "WebStore");
            }else if("name".equalsIgnoreCase(criteria)){
               newItemList = dao.searchItemByName(keyword);
            }else if("description".equalsIgnoreCase(criteria)){
                newItemList = dao.searchItemByDescription(keyword);
            }
       } catch (KwickKioskDBException ex) {
            System.out.println("Please implement proper error handling for this");
            Logger.getLogger(Cart.class.getName()).log(Level.SEVERE, null, ex);
        }
        return newItemList;
       
    }

    /**
     * This method processes search request based on a category id.
     * @param ctid
     * @return
     */
    public List<Item> searchByCategory(Integer ctid) {

         List<Item> searchResults = new ArrayList<Item>();
        try {
            ArrayList<Item> newItems = dao.getItemsByCategory(ctid, "WebStore");
            searchResults = (ArrayList<Item>) newItems.clone();
        } catch (KwickKioskDBException ex) {
            System.out.println("Please implement proper error handling for this");
            log.log(Level.SEVERE, null, ex);
        }
       return searchResults;
        
    }

   

}
